package com.smec.remes.module.drafts;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.hwangjr.rxbus.annotation.Subscribe;
import com.hwangjr.rxbus.annotation.Tag;
import com.hwangjr.rxbus.thread.EventThread;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.lcodecore.tkrefreshlayout.footer.LoadingView;
import com.smec.moblie.R;
import com.smec.moblie.SMECApplication;
import com.smec.moblie.activity.MainActivity;
import com.smec.moblie.databinding.ActivityDraftsBinding;
import com.smec.remes.base.SmecBaseFragment;
import com.smec.remes.bean.DraftsDelete;
import com.smec.remes.bean.DraftsDeleteList;
import com.smec.remes.bean.RequestSearch;
import com.smec.remes.module.drafts.adapter.SmecDraftsAdapter;
import com.smec.remes.module.request.SmecRequestFragment;
import com.smec.remes.module.submit.SmecSubmitFragment;
import com.smec.remes.smecManager.SmecRetrofit.HttpResult;
import com.smec.remes.util.CommonUtils;
import com.smec.remes.util.WmsConstant;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;


/**
 * Created by Administrator on 2017/12/7.
 */

public class SmecDraftsFragment extends SmecBaseFragment<SmecDraftsPresenter> {

    private ActivityDraftsBinding activityDraftsBinding;
    private SmecDraftsAdapter smecDraftsAdapter;
    private final static String draftsOnclick="draftsOnclick";
    private final static String DRAFTS="DRAFTS";
    private final static String DRAFTSFAIL="DRAFTSFAIL";
    private final static String DELETE="DELETE";
    private final static String DELETEFAIL="DELETEFAIL";
    private HashMap<String,Boolean> hashMap=new HashMap<>();
    private ArrayList<RequestSearch> requestSearchArrayList=new ArrayList<>();
    public static final String quanxuan="quanxuan";
    private Handler mHandler=new Handler();
    private Boolean all=false;
    private Boolean edit=false;
    private SmecRequestFragment smecRequestFragment;
    
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        activityDraftsBinding =DataBindingUtil.inflate(inflater,R.layout.activity_drafts,container,false);
        smecDraftsAdapter=new SmecDraftsAdapter(getContext());
        activityDraftsBinding.draftsRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        activityDraftsBinding.draftsRecyclerView.setAdapter(smecDraftsAdapter);
        activityDraftsBinding.getRoot().postDelayed(new Runnable() {
            @Override
            public void run() {
                ((MainActivity)getActivity()).textView_monthPicker.setText("编辑");
            }
        },300);
        _all();
        _getDrafts();
        _delete();
//        _getFragment();
        return activityDraftsBinding.getRoot();
    }

    public void _getFragment(){
        Bundle bundle = getArguments();
        String data = bundle.getString("DRAFTS");
        if(!TextUtils.isEmpty(data)){
            activityDraftsBinding.llDi.setVisibility(View.VISIBLE);
        }
    }

    @Override
    protected SmecDraftsPresenter getPersenter() {
        return new SmecDraftsPresenter(getContext());
    }

    public void _delete(){
        activityDraftsBinding.tvDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                hashMap=smecDraftsAdapter.getHashmap();
                DraftsDelete draftsDelete=new DraftsDelete();
                draftsDelete.setType("DELETE");
                ArrayList<DraftsDeleteList> applyOrderNoList=new ArrayList<>();
                ArrayList<String> arrayList=new ArrayList<>();
                Iterator iter = hashMap.entrySet().iterator();
                while (iter.hasNext()) {
                    Map.Entry entry = (Map.Entry) iter.next();
                    Object value = entry.getValue();
                    boolean newValue= (boolean) value;
                    if(newValue){
                        Object key = entry.getKey();
                        arrayList.add(String.valueOf(key));
                        DraftsDeleteList draftsDeleteList=new DraftsDeleteList();
                        draftsDeleteList.setApplyOrderNo(String.valueOf(key));
                        applyOrderNoList.add(draftsDeleteList);
                    }
                }
                draftsDelete.setApplyOrderNoList(applyOrderNoList);
                if(CommonUtils.notEmpty(applyOrderNoList)){
                    mPresenter.daftsDelete(draftsDelete);
                }else {
                    Toast.makeText(getActivity(),"请选择删除的草稿",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    public void _getDrafts(){
        activityDraftsBinding.ptr.setHeaderView(WmsConstant.getSinaRefreshView());
        activityDraftsBinding.ptr.setBottomView(new LoadingView(SMECApplication.getContext()));
        activityDraftsBinding.ptr.setAutoLoadMore(true);
        activityDraftsBinding.ptr.setEnableOverScroll(false);
        activityDraftsBinding.ptr.setEnableLoadmore(false);
        activityDraftsBinding.ptr.setOnRefreshListener(new RefreshListenerAdapter() {
            @Override
            public void onRefresh(TwinklingRefreshLayout refreshLayout) {
                super.onRefresh(refreshLayout);
                mPresenter.getDrafts();
            }
        });

        activityDraftsBinding.getRoot().postDelayed(new Runnable() {
            @Override
            public void run() {
                activityDraftsBinding.ptr.startRefresh();
            }
        },500);
    }

    public void _all(){
        activityDraftsBinding.tvAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(!all){
                    smecDraftsAdapter.setSrc(true);
                    smecDraftsAdapter.setHashMapStatus(true);
                    activityDraftsBinding.tvAll.setText("取消全选");
                    all=true;
                }else {
                    smecDraftsAdapter.setSrc(false);
                    smecDraftsAdapter.setHashMapStatus(false);
                    activityDraftsBinding.tvAll.setText("全选");
                    all=false;
                }
            }
        });
    }

    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = {
                    @Tag(DELETE)
            }
    )

    public void _deteleSuccess(HttpResult httpResult){
        if(httpResult.getCode().equals("SUCCESS")){
            Toast.makeText(getActivity(),"删除成功",Toast.LENGTH_SHORT).show();
        }
        smecDraftsAdapter.setVis(false);
        activityDraftsBinding.llDi.setVisibility(View.GONE);
        edit=false;
        ((MainActivity)getActivity()).textView_monthPicker.setText("编辑");
        smecDraftsAdapter.set_intent(true);
        activityDraftsBinding.ptr.startRefresh();
    }

    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = {
                    @Tag(DELETEFAIL)
            }
    )

    public void _deteleFail(String a){
        activityDraftsBinding.ptr.startRefresh();
    }
    
    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = {
                    @Tag(DRAFTS)
            }
    )

    public void _draftsSuccess(ArrayList<RequestSearch> arrayList){
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                activityDraftsBinding.ptr.finishRefreshing();
            }
        },200);
        if(CommonUtils.notEmpty(arrayList)){
            requestSearchArrayList=arrayList;
            smecDraftsAdapter.setArrayList(arrayList);
        }else {
            smecDraftsAdapter.clearList();
        }
    }

    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = {
                    @Tag(DRAFTSFAIL)
            }
    )

    public void _draftsfail(String a){
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                activityDraftsBinding.ptr.finishRefreshing();
            }
        },200);
    }

    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = {
                    @Tag(quanxuan)
            }
    )
    public void _quanxuan(String a){
        if(!edit){
            smecDraftsAdapter.setVis(true);
            activityDraftsBinding.llDi.setVisibility(View.VISIBLE);
            smecDraftsAdapter.setHashMap();
            smecDraftsAdapter._firstAll(true);
            edit=true;
            ((MainActivity)getActivity()).textView_monthPicker.setText("取消");
            smecDraftsAdapter.set_intent(false);
        }else {
            smecDraftsAdapter.setVis(false);
            activityDraftsBinding.llDi.setVisibility(View.GONE);
            edit=false;
            ((MainActivity)getActivity()).textView_monthPicker.setText("编辑");
            smecDraftsAdapter.set_intent(true);
        }
    }

    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = {
                    @Tag(draftsOnclick)
            }
    )

    public void _draftsOnclick(final RequestSearch requestSearch){
        ((MainActivity)getActivity()).textView_title.setText("物料申请");
        ((MainActivity)getActivity()).textView_monthPicker.setText("");
        ((MainActivity)getActivity()).textView_monthPicker.setEnabled(false);

        if(smecRequestFragment == null){
            smecRequestFragment=SmecRequestFragment.newInstance();
        }
        ((MainActivity)getActivity()).switchContent(((MainActivity)getActivity()).mContent,smecRequestFragment);
        activityDraftsBinding.getRoot().postDelayed(new Runnable() {
            @Override
            public void run() {
                mRxBus.post("RequestSearch",requestSearch);
            }
        },700);
    }

    public static SmecDraftsFragment newInstance() {
        Bundle args = new Bundle();
        SmecDraftsFragment fragment = new SmecDraftsFragment();
        fragment.setArguments(args);
        return fragment;
    }
}
